// pages/couponBuy/couponBuy.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
var voucherNum=1;
var voucherS;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tiket:"",
    totalFee:"",
    value2:1,
    can:false,
    maxNum:"",
    msg:"",
    async: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var itemJson=options.item;
    var item = JSON.parse(itemJson)
    console.log(item)
    this.setData({
      tiket:item,
      totalFee: item.amount
    })
    if (item.limit_num!=0){
      if (item.limit_num <= item.voucher_storage){
        this.setData({
          maxNum: item.limit_num,
          msg:"不能超出限购"
        })
      }else{
        this.setData({
          maxNum: item.voucher_storage,
          msg: "不能超出库存"
        })
      }
    }else{
      this.setData({
        maxNum: item.voucher_storage,
        msg: "不能超出库存"
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  onChange:function(e){
     console.log(e)
     var that=this
    var maxNum = this.setData.maxNum;
    var msg = this.data.msg;
    var totalFee = that.data.totalFee;
    var price = that.data.tiket.amount;
    var detail=e.detail;
    this.setData({
      async: true
    })
    if (detail > maxNum){
       wx.showToast({
         title: msg,
         icon: 'none',
       })
    }else{

      totalFee = (detail * price).toFixed(2);
       this.setData({
         value2: detail,
         totalFee: totalFee
       })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     var that=this;
     wx.getStorage({
       key: 'token',
       success: function(res) {
         token=res.data
       },
     })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  plus: function (e) {
    var that=this;
    that.setData({
      can:true
    })
    var totalFee = that.data.totalFee;
    var price = that.data.tiket.amount;
    var value2=that.data.value2;
    value2++
    totalFee = (value2 * price).toFixed(2);
    that.setData({
      value2: value2,
      totalFee: totalFee,
      can: false,
      async: false
    })
  },
  minus: function (e) {
    var that = this;
    that.setData({
      can: true
    })
    var totalFee = that.data.totalFee;
    var price = that.data.tiket.amount;
    var value2 = that.data.value2;
    value2--
    totalFee = (value2 * price).toFixed(2);
    that.setData({
      value2: value2,
      totalFee: totalFee,
      can: false,
      async: false
    })
  },
  overlimit:function(e){
    var detail=e.detail;
    var msg=this.data.msg
    if (detail =="plus"){
       wx.showToast({
         title: msg,
         icon: 'none',
       })
    }
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  tobuy: function (e) {
    var that = this;
    var voucherId = that.data.tiket.id;
    var voucherNum=that.data.value2;
    // var voucherNum = that.data.tiket.voucherNum;
    wx.request({
      url: url.URl + 'goods/voucher/tobuy',
      data: { id: voucherId, num: voucherNum },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        var msg = res.data.msg;
        wx.hideLoading()
        console.log(res)
        if (res.data.code == 201) {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        } else {
          wx.requestPayment({
            'appId': res.data.appId,
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': function (res) {
              that.setData({
                disabled: false
              })
              console.log(res)
              wx.showToast({
                title: "支付成功",
                icon: 'success',
                duration: 2000,
                success: function () {
                   wx.navigateTo({
                     url: '/pages/success/success?type=4',
                     success: function(res) {},
                     fail: function(res) {},
                     complete: function(res) {},
                   })
                }
              })
            },
            fail: function () {
              that.setData({
                disabled: false
              })

            }
          })
        }
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})